﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Mammoo.Ajax
{
    /// <summary>
    /// 
    /// </summary>
    public class AjaxHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// OP
        /// </summary>
        public string op = "";

        /// <summary>
        /// 要检查的SessionKey
        /// </summary>
        public string SessionKey = "admin";

        /// <summary>
        /// 是否要检查登录
        /// </summary>
        public bool CheckLoginIn = true;

        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            op = context.Request["op"].ToLower();
            if (string.IsNullOrEmpty(op))
            {
                AjaxService.WriteTimeOut(context);
                return;
            }

            if (CheckLoginIn)
            {
                if (context.Session[SessionKey] == null)
                {
                    AjaxService.WriteTimeOut(context);
                    return;
                }
            }

            try
            {
                switch (op)
                {
                    case "getlist":
                        GetList(context);
                        break;
                    case "getcombo":
                        GetCombo(context);
                        break;
                    case "gettree":
                        GetTree(context);
                        break;
                    case "add":
                        Add(context);
                        break;
                    case "del":
                        Del(context);
                        break;
                    case "edit":
                        Edit(context);
                        break;
                    case "save":
                        Save(context);
                        break;
                    default:
                        RequestProcess(context);
                        break;
                }
            }
            catch (Exception e)
            {
                context.Response.Write(AjaxService.WriteFail(e.Message, ""));
            }
        }



        #region 虚方法

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="context"></param>
        public virtual void GetList(HttpContext context)
        {
        }

        /// <summary>
        /// 获取Combo下拉
        /// </summary>
        /// <param name="context"></param>
        public virtual void GetCombo(HttpContext context)
        {
        }

        /// <summary>
        /// 获取Tree
        /// </summary>
        /// <param name="context"></param>
        public virtual void GetTree(HttpContext context)
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="context"></param>
        public virtual void Add(HttpContext context)
        {
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="context"></param>
        public virtual void Del(HttpContext context)
        {
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="context"></param>
        public virtual void Edit(HttpContext context)
        {
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="context"></param>
        public virtual void Save(HttpContext context)
        {
        }

        /// <summary>
        /// 子类重写该方法，重新匹配op
        /// </summary>
        /// <param name="context"></param>
        public virtual void RequestProcess(HttpContext context)
        {
        }

        #endregion



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        


        #region IHttpHandler 成员

        bool IHttpHandler.IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
